package service

import (
	"encoding/json"
	"fmt"
	"net/http"
	"strconv"
	"webapp/model"
)

func GetHardWareClassListService(w http.ResponseWriter, r *http.Request) {
	r.ParseMultipartForm(1>>64 - 1)
	form := r.Form
	token := form.Get("token")
	userVerify := VerifyUser(token)
	if !userVerify {
		jstr, _ := json.Marshal(&model.GetHWClassListRet{Success: false, Logout: true})
		fmt.Fprintf(w, string(jstr))
		return
	}

	list, err := model.GetHardwareClassList()
	if err != nil {
		jstr, _ := json.Marshal(&model.GetHWClassListRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}

	jstr, _ := json.Marshal(&model.GetHWClassListRet{Success: true, Logout: false, HWClassList: list})
	fmt.Fprintf(w, string(jstr))
	return
}

func AddHardWareClassService(w http.ResponseWriter, r *http.Request) {
	r.ParseMultipartForm(1>>64 - 1)
	form := r.Form
	token := form.Get("token")
	managerVerify := VerifyManager(token)
	if !managerVerify {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: true})
		fmt.Fprintf(w, string(jstr))
		return
	}
	className := form.Get("class_name")
	newClass := &model.HardwareClass{model.GetHardwareClassNewId(), className}
	err := newClass.AddHardWareClass()
	if err != nil {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}
	jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: true, Logout: false})
	fmt.Fprintf(w, string(jstr))
	return
}

func DeleteHardWareClassService(w http.ResponseWriter, r *http.Request) {
	r.ParseMultipartForm(1>>64 - 1)
	form := r.Form
	token := form.Get("token")
	managerVerify := VerifyManager(token)
	if !managerVerify {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: true})
		fmt.Fprintf(w, string(jstr))
		return
	}
	classId := form.Get("class_id")

	id, err := strconv.Atoi(classId)
	if err != nil {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}
	newClass := &model.HardwareClass{ClassId: id}
	err = newClass.DeleteHardWareClass()
	if err != nil {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}
	jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: true, Logout: false})
	fmt.Fprintf(w, string(jstr))
	return
}

func AddHardWareItemService(w http.ResponseWriter, r *http.Request) {

	len := r.ContentLength    // 获取请求实体长度
	body := make([]byte, len) // 创建存放请求实体的字节切片
	r.Body.Read(body)         // 调用 Read 方法读取请求实体并将返回内容存放到上面创建的字节切片
	var resp map[string]any
	json.Unmarshal(body, &resp) // 对读取的 JSON 数据进行解析
	//fmt.Println(resp)           // 格式化输出结果
	token := resp["token"].(string)
	if !VerifyManager(token) {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: true})
		fmt.Fprintf(w, string(jstr))
		return
	}
	itemName, ok := resp["ItemName"].(string)
	if !ok {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}
	classId := int(resp["classId"].(float64))
	itemAttr := make(map[string]string)
	itemAttrArray, ok := resp["ItemAttr"].([]any)
	if !ok {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}
	for _, each := range itemAttrArray {
		m := each.(map[string]any)
		attrName := m["AttrName"].(string)
		attrVal := m["AttrVal"].(string)
		itemAttr[attrName] = attrVal
	}

	item := &model.HardwareItem{ItemId: model.GetHardwareItemNewId(), ItemName: itemName, ClassId: classId, ItemAttr: itemAttr}
	err := item.AddHardwareItem()
	if err != nil {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}

	jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: true, Logout: false})
	fmt.Fprintf(w, string(jstr))
	return

}

func GetHardWareItemListService(w http.ResponseWriter, r *http.Request) {
	r.ParseMultipartForm(1>>64 - 1)
	form := r.Form
	token := form.Get("token")
	managerVerify := VerifyUser(token)
	if !managerVerify {
		jstr, _ := json.Marshal(&model.HWItemListRet{Success: false, Logout: true})
		fmt.Fprintf(w, string(jstr))
		return
	}
	list, err := model.GetHardwareItemList()
	if err != nil {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}

	jstr, _ := json.Marshal(&model.HWItemListRet{Success: false, Logout: true, HWItemList: list})
	fmt.Fprintf(w, string(jstr))
	return
}

func DeleteHardWareItemService(w http.ResponseWriter, r *http.Request) {
	r.ParseMultipartForm(1>>64 - 1)
	form := r.Form
	token := form.Get("token")
	managerVerify := VerifyManager(token)
	if !managerVerify {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: true})
		fmt.Fprintf(w, string(jstr))
		return
	}
	itemId := form.Get("item_id")
	itemIdInt, err := strconv.Atoi(itemId)
	if err != nil {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}

	item := &model.HardwareItem{ItemId: itemIdInt}
	err = item.DeleteHardwareItem()
	if err != nil {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}

	jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: true, Logout: false})
	fmt.Fprintf(w, string(jstr))

	return
}

func ModifyHardWareClassNameService(w http.ResponseWriter, r *http.Request) {
	r.ParseMultipartForm(1>>64 - 1)
	form := r.Form
	token := form.Get("token")
	managerVerify := VerifyManager(token)
	if !managerVerify {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: true})
		fmt.Fprintf(w, string(jstr))
		return
	}
	className := form.Get("new_class_name")
	classId := form.Get("class_id")
	classIdInt, err := strconv.Atoi(classId)
	if err != nil {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}

	modify := &model.HardwareClass{ClassId: classIdInt, ClassName: className}
	err = modify.ModifyHardwareClass()
	if err != nil {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}
	GetHardWareClassListService(w, r)
	return
}

func ModifyHardwareItemService(w http.ResponseWriter, r *http.Request) {
	r.ParseMultipartForm(1>>64 - 1)
	form := r.Form
	token := form.Get("token")
	managerVerify := VerifyManager(token)
	if !managerVerify {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: true})
		fmt.Fprintf(w, string(jstr))
		return
	}
	newClassId := form.Get("new_class_id")
	newItemName := form.Get("new_item_name")
	itemId := form.Get("item_id")
	classIdInt, err := strconv.Atoi(newClassId)
	if err != nil {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}
	itemIdInt, err := strconv.Atoi(itemId)
	if err != nil {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}
	modify := &model.HardwareItem{ItemId: itemIdInt, ClassId: classIdInt, ItemName: newItemName}
	err = modify.ModifyHardwareItem()
	if err != nil {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}

	list, err := model.GetHardwareItemList()
	if err != nil {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}
	jstr, _ := json.Marshal(&model.HWItemListRet{Success: true, Logout: false, HWItemList: list})
	fmt.Fprintf(w, string(jstr))
	return
}

func ModifyHardwareItemAttrService(w http.ResponseWriter, r *http.Request) {
	r.ParseMultipartForm(1>>64 - 1)
	form := r.Form
	token := form.Get("token")
	managerVerify := VerifyManager(token)
	if !managerVerify {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: true})
		fmt.Fprintf(w, string(jstr))
		return
	}
	itemId := form.Get("item_id")
	attrName := form.Get("attr_name")
	newAttrVal := form.Get("new_attr_val")
	itemIdInt, err := strconv.Atoi(itemId)
	if err != nil {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}

	model.ModifyHardwareItemAttr(itemIdInt, attrName, newAttrVal)
	if err != nil {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}

	list, err := model.GetHardwareItemList()
	if err != nil {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}
	jstr, _ := json.Marshal(&model.HWItemListRet{Success: true, Logout: false, HWItemList: list})
	fmt.Fprintf(w, string(jstr))
	return
}

func AddHardWareItemAttrService(w http.ResponseWriter, r *http.Request) {
	r.ParseMultipartForm(1>>64 - 1)
	form := r.Form
	token := form.Get("token")
	managerVerify := VerifyManager(token)
	if !managerVerify {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: true})
		fmt.Fprintf(w, string(jstr))
		return
	}
	itemId := form.Get("item_id")
	attrName := form.Get("attr_name")
	attrVal := form.Get("attr_val")
	itemIdInt, err := strconv.Atoi(itemId)
	if err != nil {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}
	err = model.AddHardwareItemAttr(itemIdInt, attrName, attrVal)
	if err != nil {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}
	jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: true, Logout: false})
	fmt.Fprintf(w, string(jstr))
	return
}

func DeleteHardWareItemAttrService(w http.ResponseWriter, r *http.Request) {
	r.ParseMultipartForm(1>>64 - 1)
	form := r.Form
	token := form.Get("token")
	managerVerify := VerifyManager(token)
	if !managerVerify {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: true})
		fmt.Fprintf(w, string(jstr))
		return
	}
	itemId := form.Get("item_id")
	attrName := form.Get("attr_name")
	itemIdInt, err := strconv.Atoi(itemId)
	if err != nil {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}
	err = model.DeleteHardwareItemAttr(itemIdInt, attrName)
	if err != nil {
		jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: false, Logout: false})
		fmt.Fprintf(w, string(jstr))
		return
	}
	jstr, _ := json.Marshal(&model.SuccessAndLogoutRet{Success: true, Logout: false})
	fmt.Fprintf(w, string(jstr))
	return
}
